package type;

import java.util.ArrayList;
import java.util.List;

/**
 * @Date 2021/4/29 11:59 上午
 * @Author dengxin
 */
public class Generic5<T> {

    static class AddCountInteger {
        static int initial;
        final int count = initial++;

        @Override
        public String toString() {
            return Integer.toString(count);
        }
    }

    static class SubCountInteger {
        static int initial = 10;
        final int count = initial--;

        @Override
        public String toString() {
            return Integer.toString(count);
        }
    }

    Class<T> type;

    public Generic5(Class<T>  type) {
        this.type = type;
    }

    private void fillList(int elements) throws IllegalAccessException, InstantiationException {
        List<T> list = new ArrayList<>();
        for (int i = 0; i< elements; i++) {
            list.add(type.newInstance());
        }
        System.out.println(list);
    }

    public static void main(String[] args) throws InstantiationException, IllegalAccessException {
        Generic5<AddCountInteger> generic5 = new Generic5<>(AddCountInteger.class);
        generic5.fillList(10);
        Generic5<SubCountInteger> generic5_1 = new Generic5<>(SubCountInteger.class);
        generic5_1.fillList(8);
    }
}
